package com.wyl.spider.pipline;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wyl.model.Simple;
import com.wyl.service.SimpleService;
import com.wyl.utils.Utils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Component
public class SimplePipeline implements Pipeline {

	private HashMap<String,String> map = (HashMap<String, String>) Utils.getUrlMap();

	@Autowired
	private SimpleService simpleService ;
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		String data = resultItems.get("data").toString();
		String channel = map.get(resultItems.get("channel").toString());
		List<Simple> simpleList = Utils.transToSimple(Utils.getNewsList(data));
		for (Simple simple2 : simpleList) {
			simple2.setChannelname(channel);
			simpleService.save(simple2);
		}
	}

}
